
package ch.hearc.meteo.imp.afficheur.real.vue.atome;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;

import ch.hearc.meteo.imp.afficheur.real.JFrameStationMeteo;
import ch.hearc.meteo.imp.afficheur.real.moo.StationMeteoElement;

public abstract class JPanelGraph extends JPanel
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public JPanelGraph(ArrayList<StationMeteoElement> listeStationMeteoElement, String axeX, String axeY)
		{
		this.listeStationMeteoElement = listeStationMeteoElement;
		chart = ChartFactory.createTimeSeriesChart("", axeX, axeY, null, false, true, true);

		XYPlot xyPlot = chart.getXYPlot();
		xyPlot.setBackgroundPaint(JFrameStationMeteo.COLOR_S3);
		chart.setBackgroundPaint(JFrameStationMeteo.COLOR_S3);
		chart.getTitle().setPaint(JFrameStationMeteo.COLOR_P2);

		DateAxis dateAxis = (DateAxis)xyPlot.getDomainAxis();
		dateAxis.setTickLabelPaint(JFrameStationMeteo.COLOR_BLACK);
		dateAxis.setLabelPaint(JFrameStationMeteo.COLOR_P1);
		dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));

		xyPlot.getRangeAxis().setTickLabelPaint(JFrameStationMeteo.COLOR_BLACK);
		xyPlot.getRangeAxis().setLabelPaint(JFrameStationMeteo.COLOR_P1);
		xyPlot.setRenderer(1, new StandardXYItemRenderer());

		chartPanel = new ChartPanel(chart)
			{

				@Override
				public void paintComponent(Graphics g)
					{
					super.paintComponent(g);
					Dimension size = chartPanel.getSize();
					int w = (int)Math.rint(size.width);
					int h = (int)Math.rint(size.height);
					chart.draw((Graphics2D)g, new Rectangle2D.Double(0, 0, w, h));
					}
			};

		this.setLayout(new BorderLayout());
		this.add(chartPanel, BorderLayout.CENTER);

		refresh();
		}

	@Override
	public Font getFont()
		{
		if (chart != null)
			{
			return chart.getTitle().getFont();
			}
		else
			{
			return super.getFont();
			}
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/
	public abstract void refresh();

	public void updateColors()
		{
		int i = 0;
		XYItemRenderer xyItemRenderer = chart.getXYPlot().getRenderer();
		synchronized (listeStationMeteoElement)
			{
			for(StationMeteoElement stationMeteoElement:listeStationMeteoElement)
				{
				stationMeteoElement.setGraphColor((Color)chart.getXYPlot().getRenderer().getSeriesPaint(i));

				i++;
				}
			}
		chart.getXYPlot().setRenderer(xyItemRenderer);
		}

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	protected ArrayList<StationMeteoElement> listeStationMeteoElement;
	protected JFreeChart chart;
	protected ChartPanel chartPanel;

	}
